#include "frmPhysics.h"
#include "GameSystemManager.h"
#include "Physics.h"
#include "GUISystem.h"

frmPhysics::frmPhysics() {
	subscribeEvents();
	setup();
}
frmPhysics::~frmPhysics() {

}
	
void frmPhysics::subscribeEvents() {
	////////////////////////////////////////////////////////////////////////////////////////////////////////////
	//Subsribe GUI Physics events
	CEGUI::Window* btnStart = CEGUI::WindowManager::getSingleton().getWindow("Physics/btnRun");
	btnStart->subscribeEvent(CEGUI::PushButton::EventClicked,CEGUI::Event::Subscriber(&frmPhysics::evtPhysicsStart_click,this));

	CEGUI::Window* btnStop = CEGUI::WindowManager::getSingleton().getWindow("Physics/btnStop");
	btnStop->subscribeEvent(CEGUI::PushButton::EventClicked,CEGUI::Event::Subscriber(&frmPhysics::evtPhysicsStop_click,this));

	CEGUI::Window* btnCreate = CEGUI::WindowManager::getSingleton().getWindow("Physics/btnCreate");
	btnCreate->subscribeEvent(CEGUI::PushButton::EventClicked,CEGUI::Event::Subscriber(&frmPhysics::evtPhysicsCreate_click,this));

	CEGUI::Window* btnDestroy = CEGUI::WindowManager::getSingleton().getWindow("Physics/btnDestroy");
	btnDestroy->subscribeEvent(CEGUI::PushButton::EventClicked,CEGUI::Event::Subscriber(&frmPhysics::evtPhysicsDestroy_click,this));

	//CEGUI::Window* btnGenerateTerrain = CEGUI::WindowManager::getSingleton().getWindow("Physics/btnTerrainActor");
	//btnGenerateTerrain->subscribeEvent(CEGUI::PushButton::EventClicked,CEGUI::Event::Subscriber(&frmPhysics::evtPhysicsGenerateTerrainActor_click,this));

	CEGUI::Window* wndPhysicsClose = CEGUI::WindowManager::getSingleton().getWindow("Physics");
	wndPhysicsClose->subscribeEvent(CEGUI::FrameWindow::EventCloseClicked,CEGUI::Event::Subscriber(&frmPhysics::evtPhysicsClose_click,this));

}

void frmPhysics::setup() {
	CEGUI::Window* w = CEGUI::WindowManager::getSingleton().getWindow("Physics");
	w->setVisible(true);
}

void frmPhysics::fillConfigFromGui() {

	//GameSystemManager::getSingleton()->

	physics_config.Gravity = GameSystemManager::getSingleton()->getGUI()->EditBoxToFloat("Physics/txtGravity");
	physics_config.Skin = GameSystemManager::getSingleton()->getGUI()->EditBoxToFloat("Physics/txtSkinWidth");
	physics_config.Restitution = GameSystemManager::getSingleton()->getGUI()->EditBoxToFloat("Physics/txtRestitution");
	physics_config.StaticFriction = GameSystemManager::getSingleton()->getGUI()->EditBoxToFloat("Physics/txtStaticFriction");
	physics_config.DynamicFriction = GameSystemManager::getSingleton()->getGUI()->EditBoxToFloat("Physics/txtDynamicFriction");

}

//////////////////////////////////////////////////////////////////////////////////////////////////////
bool frmPhysics::evtPhysicsClose_click(const CEGUI::EventArgs& e)
{
	CEGUI::Window* w = CEGUI::WindowManager::getSingleton().getWindow("Physics");
	w->setVisible(false);
	return true;
}
bool frmPhysics::evtPhysicsStart_click(const CEGUI::EventArgs& e)
{
	GameSystemManager::getSingleton()->getPhysics()->startSimulation();
	physics_config.isSimulation = true;
	return true;
}
bool frmPhysics::evtPhysicsStop_click(const CEGUI::EventArgs& e)
{
	GameSystemManager::getSingleton()->getPhysics()->stopSimulation();
	physics_config.isSimulation = false;
	return true;
}
bool frmPhysics::evtPhysicsCreate_click(const CEGUI::EventArgs& e)
{
	GameSystemManager::getSingleton()->getGUI()->showLoader(true);
	fillConfigFromGui();
	GameSystemManager::getSingleton()->getPhysics()->createScene();
	GameSystemManager::getSingleton()->getGUI()->showLoader(false);
	return true;
}
bool frmPhysics::evtPhysicsDestroy_click(const CEGUI::EventArgs& e)
{
	GameSystemManager::getSingleton()->getGUI()->showLoader(true);
	//GameSystemManager::getSingleton()->getPhysics()->shutdown();
	GameSystemManager::getSingleton()->getGUI()->showLoader(false);
	return true;
}
